<?php
define('WB_TTYPE_T_IN', '疏港'); // 集卡进口
define('WB_TTYPE_T_OUT', '集港'); // 集卡出口
define('WB_TTYPE_ZC', '整车'); // 整车
define('WB_TTYPE_LD', '零担'); // 零担
define('WB_TTYPE_CP', '城配'); // 城配
class WaybillModel extends BaseModel
{
    var $table  = 'waybill_main';
    var $prikey = 'wm_tid';
    var $_name  = 'waybill';
    
var $_relation  = array(

        
        'has_trunk' => array(
            'model'         => 'waybilltrunk',
            'type'          => HAS_MANY,
            'foreign_key'   => 'ws_wmtid',
            'dependent'     => true
        ),
        // 一个运单有多个监控
        'has_normal' => array(
            'model'         => 'waybillnormal',
            'type'          => HAS_MANY,
            'foreign_key'   => 'ws_wmtid',
            'dependent'     => true
        ),
        // 一个运单有多个运程
        'has_haul' => array(
            'model'         => 'haul',
            'type'          => HAS_MANY,
            'foreign_key'   => 'h_wmtid',
            'dependent'     => true
        ),
        // 一个运单有多个监控
        'has_monitor' => array(
            'model'         => 'monitor',
            'type'          => HAS_MANY,
            'foreign_key'   => 'm_zmmtid',
            'dependent'     => true
        ),
        'belongs_to_user' => array(
            'model'         => 'member',
            'type'          => BELONGS_TO,
            'foreign_key'   => 'user_id',
            'reverse'       => 'has_will',
        ),
    );
 /**
     * 推送消息
     * @param $w
     */
    function pushmessage($w)
    {
        $member_mod=&m("member");
        $user=$member_mod->get($w['wm_user']);
        //发送推送消息
        require_once ROOT_PATH . '/baidupush/sdk.php';
        $sdk = new PushSDK();
        $channelId = $user['channelId'] ? $user['channelId'] : '3849309310203392735';
        $wid=$w['wm_tid'];
        try {
            $opts = array(
                'msg_type' => 0
            );
            if ($w['wm_type'] == '疏港') {
                $sql = "select * from zys_goods_trunk where exists(select 1 from zys_waybill_sub_trunk where g_tid=ws_gid and ws_wmtid=" . $wid . ")";
                $gt = $this->getRow($sql);

                $rs = $sdk->pushMsgToSingleDevice($channelId,
                    '{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_collectiontime']) . '到' . $gt['g_full_container_depot'] . '背重箱
				' . local_date('Y年m月d日 H点i分', $gt['g_packingtime']) . '送至' . $gt['g_loading_place'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
            } else if ($w['wm_type'] == '集港') {
                $sql = "select * from zys_goods_trunk where exists(select 1 from zys_waybill_sub_trunk where g_tid=ws_gid and ws_wmtid=" . $wid . ")";
                $gt = $this->getRow($sql);

                $rs = $sdk->pushMsgToSingleDevice($channelId,
                    '{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_packingtime']) . '到' . $gt['g_loading_place'] . '装货
				' . local_date('Y年m月d日 H点i分', $gt['g_collectiontime']) . '前送至' . $gt['g_full_container_depot'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
            } else {
                $sql = "select * from zys_goods_normal where exists(select 1 from zys_waybill_sub_normal where g_tid=ws_gid and ws_wmtid=" . $wid . ") order by g_opdate desc limit 1";
                $gt = $this->getRow($sql);

                if ($gt) {
                    $rs = $sdk->pushMsgToSingleDevice($channelId,
                        '{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_loading_date']) . '到' . $gt['g_loading_place'] . '提货
				' . local_date('Y年m月d日 H点i分', $gt['g_unloading_date']) . '送至' . $gt['g_unloading_place'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
                }
            }
        }
        catch(Exception $exp){}
    }
}
?>